package xdrawer.test;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import java.util.ArrayList;


public class MyPane extends BorderPane {
    private Pane picturePane;
    private HBox toobBox;
    private ImageView viewer;
    private Button b1, b2;
    private ArrayList<String> list;
    private int currentIndex = 0;

    private double startMouseX;
    private double startMouseY;
    private double startImageX, startImageY;

    public MyPane() {
        //创建
        picturePane = new Pane();
        viewer = new ImageView();
        //viewer.setImage(new Image("file:export.png", 500,400,true,true));
//        viewer.setImage(new Image(list.get(currentIndex)));

        picturePane.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                startMouseX = event.getX();
                startMouseY = event.getY();
                startImageX = viewer.getLayoutX();
                startImageY = viewer.getLayoutY();

                System.out.println(startMouseX + ", " + startMouseY);
            }
        });

        picturePane.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                double deltaX = event.getX() - startMouseX;
                double deltaY = event.getY() - startMouseY;

                viewer.setLayoutX(startImageX + deltaX);
                viewer.setLayoutY(startImageY + deltaY);
            }
        });

        toobBox = new HBox();
        b1 = new Button("Prev");
        b2 = new Button("Next");

        //组合
        toobBox.getChildren().addAll(b1, b2);
        picturePane.getChildren().add(viewer);
        viewer.setLayoutX(0);
        viewer.setLayoutY(0);

        this.setCenter(picturePane);
        this.setBottom(toobBox);

        //动作
        b1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                if (currentIndex > 0) {
                    System.out.println("previous picture");
                    currentIndex--;
                } else {
                    System.out.println("The First");
                }
                viewer.setImage(new Image("File:" + list.get(currentIndex)));

                System.out.println("i=" + currentIndex);
            }
        });

        b2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                if (currentIndex < list.size() - 1) {
                    currentIndex++;
                    System.out.println("next picture");
                } else {
                    System.out.println("The Last");
                }
                viewer.setImage(new Image("File:" + list.get(currentIndex)));
                System.out.println("i=" + currentIndex);
            }
        });
    }

    public void setList(ArrayList list) {
        this.list = list;
    }

    public void setCurrentIndex(int index) {
        this.currentIndex = index;
        viewer.setImage(new Image("File:" + list.get(currentIndex)));
    }
}
